using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 控制摄像机视角，可转换朝向

public class RotateCamera : MonoBehaviour
{
    // Start is called before the first frame update
    public float rotateTime = 0.2f;
    private Transform player;
    private bool isRotate = false;
    private Vector3 offset;
    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player").transform;
        offset = transform.position - player.position;
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = player.position + offset;

        Rotate();
    }

    void Rotate(){
        if(Input.GetKeyDown(KeyCode.Q) && !isRotate){
            StartCoroutine(RotateAround(-45, rotateTime));
        }
        if(Input.GetKeyDown(KeyCode.E) && !isRotate){
            StartCoroutine(RotateAround(45, rotateTime));
        }
    }

    IEnumerator RotateAround(float angel, float time){
        float number = 60 * time;
        float nextAngel = angel / number;
        isRotate = true;

        for(int i = 0;i < number;i ++){
            transform.Rotate(new Vector3(0,0,nextAngel));
            yield return new WaitForFixedUpdate();
        }

        isRotate = false;
    }
}
